package com.slmxz.tms.biz.controller;

import com.slmxz.tms.biz.model.entity.Order;
import com.slmxz.tms.biz.model.vo.OrderVO;
import com.slmxz.tms.biz.service.OrderService;
import com.slmxz.tms.common.annotation.Log;
import com.slmxz.tms.common.enums.LogModuleEnum;
import com.slmxz.tms.common.support.Condition;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.slmxz.tms.common.result.PageResult;
import com.slmxz.tms.common.result.Result;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import jakarta.validation.Valid;
import com.slmxz.tms.common.model.Query;

/**
 * 订单前端控制层
 *
 * @author sunyao
 *@Date 2024-10-08 16:58
 */
@Tag(name = "订单接口")
@RestController
@RequestMapping("/biz/order")
@RequiredArgsConstructor
public class OrderController  {

    private final OrderService orderService;

    @Operation(summary = "订单分页列表")
    @GetMapping("/page")
    public PageResult<OrderVO> selectOrderPage(Query query, OrderVO order) {
        IPage<OrderVO> result = this.orderService
                .selectOrderPage(Condition.getPage(query),order);
        return PageResult.success(result);
    }

    @Operation(summary = "新增订单")
    @PostMapping("/save")
    @PreAuthorize("@ss.hasPerm('biz:order:add')")
    @Log(value = "新增订单", module = LogModuleEnum.ORDER)
    public Result<Void> saveOrder(@RequestBody @Valid OrderVO  order) throws Exception {
        boolean result = this.orderService.saveOrder(order);
        return Result.result(result);
    }

    @Operation(summary = "获取订单详情数据")
    @GetMapping("/detail/{id}")
    @PreAuthorize("@ss.hasPerm('biz:order:edit')")
    public Result<Order> selectOrderDetail(
        @Parameter(description = "订单ID") @PathVariable Long id
    ) {
        Order order = this.orderService.selectOrderDetail(id);
        return Result.success(order);
    }

    @Operation(summary = "修改订单")
    @PutMapping(value = "/update")
    @PreAuthorize("@ss.hasPerm('biz:order:edit')")
    @Log(value = "修改订单", module = LogModuleEnum.ORDER)
    public Result<?> updateOrder(@RequestBody @Validated OrderVO order) throws Exception {
        boolean result = orderService.updateOrder(order);
        return Result.result(result);
    }

    @Operation(summary = "审核订单")
    @PutMapping(value = "/check")
    @PreAuthorize("@ss.hasPerm('biz:order:check')")
    @Log(value = "审核订单", module = LogModuleEnum.ORDER)
    public Result<?> checkOrder(@RequestBody OrderVO order) throws Exception {
        boolean result = orderService.checkOrder(order);
        return Result.result(result);
    }

    @Operation(summary = "删除订单")
    @DeleteMapping("/remove/{ids}")
    @PreAuthorize("@ss.hasPerm('biz:order:delete')")
    @Log(value = "删除订单", module = LogModuleEnum.ORDER)
    public Result<?> deleteOrders(
        @Parameter(description = "订单ID，多个以英文逗号(,)分割") @PathVariable String ids
    ) {
        boolean result = this.orderService.deleteOrders(ids);
        return Result.result(result);
    }

    @Operation(summary = "激活或者注销订单")
    @PostMapping("/activeOrLogout")
    @Log(value = "激活或者注销订单", module = LogModuleEnum.ORDER)
    @PreAuthorize("@ss.hasPerm('document:business:delete')")
    public Result<?> activeOrLogout(@RequestBody OrderVO order) {
        boolean result = this.orderService.activeOrLogout(order);
        return Result.result(result);
    }
}
